package com.example.controller;


import com.example.common.Result;
import com.example.domain.Account;
import com.example.domain.Employee;
import com.example.exception.CustomException;
import com.example.service.AdminService;
import com.example.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class WebController {
    @Autowired
    EmployeeService employeeService;

    @Autowired
    AdminService adminService;

    @GetMapping("/hello")
    public String hello(){
        return "Hello";
    }

//    判断是登录身份
    @PostMapping("/login")
    public Result login(@RequestBody Account account){
        Account result = null;
        if ("ADMIN".equals(account.getRole())){     //管理员登陆
            result = adminService.login(account);

        }else if ("EMP".equals(account.getRole())){    //员工
            result = employeeService.login(account);
        }else {
            throw new CustomException("500","非法输入");
        }
        return Result.success(" ",result);
    }

    //    员工注册
        @PostMapping("/register")
        public Result register(@RequestBody Employee employee){
            employeeService.register(employee);
            return Result.success("成功",null);
    }

//    修改密码
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account){

        if ("ADMIN".equals(account.getRole())){     //管理员登录
            adminService.updatePassword(account);
        } else if ("EMP".equals(account.getRole())) {   //员工
            employeeService.updatePassword(account);
        }else {
            throw new CustomException("500","非法输入");
        }

        return Result.success("成功",null);

    }


}
